package mysql

import (
	"gorm.io/gorm"
	"hema-fresh/hemarpc/hemarpc"
)

type Shop struct {
	gorm.Model
	Title   string  `gorm:"type:varchar(50);comment:'商品标题'"`
	Price   float64 `gorm:"type:decimal(10,2);comment:商品价格"`
	Stock   int64   `gorm:"type:int(11);comment:'商品库存'"`
	ClassId int64   `gorm:"type:int;comment:'分类ID'"`
}

func (s *Shop) ShopAdd(db *gorm.DB) error {
	return db.Debug().Create(s).Error
}

func (s *Shop) FindShopByTitle(db *gorm.DB, title string) error {
	return db.Debug().Where("title = ?", title).First(s).Error
}

func (s *Shop) ShopList(db *gorm.DB, list []*hemarpc.ShopList, in *hemarpc.ShopListRequest) ([]*hemarpc.ShopList, error) {
	err2 := db.Debug().Model(&Shop{}).Select(
		"shops.id as shops_id",
		"shops.title",
		"shops.price",
		"shops.stock",
		"shops.class_id",
	).Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list).Error
	return list, err2
}
